package com.example.userclient.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.example.userclient.AddBook
import com.example.userclient.BookRemark
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.remote.Requester
import com.example.userclient.adapter.MessageAdapter
import com.example.userclient.databinding.ActivityTalkBinding
import com.example.userclient.remote.AppExecutors

class TalkActivity : AppCompatActivity() {

    private var talkList: RecyclerView? = null
    private val mHandler = Handler(Looper.getMainLooper()) {
        if (it.what == 0){
            Toast.makeText(this,"网络请求错误",Toast.LENGTH_SHORT).show()
        }else if(it.what == 1){
            Toast.makeText(this,"发送成功",Toast.LENGTH_SHORT).show()
            talkList?.smoothScrollToPosition(MessageAdapter.itemCount)
        }
        false
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bind =  DataBindingUtil.setContentView<ActivityTalkBinding>(this, R.layout.activity_talk)

        val book = intent.extras?.get("addBook") as AddBook

        val sendTo = book.bookOwner

        val sendFrom = ClientInfoHolder.phone!!
//        Log.i("Fatal", book.self_id)

//        Log.i("talkActivity"," $sendTo $sendFrom")
        talkList = bind.talkList
        bind.talkList.layoutManager = LinearLayoutManager(this)

        MessageAdapter.cur_talk = sendTo

        MessageAdapter.cur_talk_book = book.id

//        ClientInfoHolder.personRemark?.forEach{
//            Log.i("talkpersonRemark" ," ${it.sendFrom} ${it.sendTo}")
//        }
        MessageAdapter.dataSet = MessageAdapter.clean(ClientInfoHolder.personRemark)

//        MessageAdapter.dataSet?.forEach {
//            Log.i("talkdataSet"," ${it.sendFrom} ${it.sendTo}")
//        }

        bind.talkList.adapter = MessageAdapter
        (bind.talkList.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false

        bind.talkBack.setOnClickListener {
            finish()
        }

        bind.sendButton.setOnClickListener{
            val b  = bind.inputText.text.toString()
            if (b.isEmpty()){
                Toast.makeText(this,"信息不能为空",Toast.LENGTH_SHORT).show()
            }
            bind.inputText.text.clear()
            val bookRemark = BookRemark()
            bookRemark.content  = b
            bookRemark.sendFrom = sendFrom
            bookRemark.sendTo = sendTo
            bookRemark.sendTime  = System.currentTimeMillis()
            bookRemark.isRead = false
            MessageAdapter.dataSet?.add(bookRemark)

            MessageAdapter.notifyItemInserted(MessageAdapter.itemCount)
            AppExecutors.startTask(
                    Thread{
                        val result =  Requester.apiService().addRemark(sendFrom,sendTo,b,book.id,book.self_id).execute()

                        if (result.isSuccessful){
                           mHandler.sendEmptyMessage(1)


                        }else{
                            mHandler.sendEmptyMessage(0)
                        }
                    }
            )

        }

    }




    companion object{
        fun actionStart(context: Context,addBook: AddBook){
            val intent = Intent(context, TalkActivity::class.java)
            intent.putExtra("addBook",addBook)
            context.startActivity(intent)

        }
        fun actionStart2(context: Context, addBook: AddBook, talkWith:String){
            val intent = Intent(context, TalkActivity::class.java)
            addBook.bookOwner = talkWith
            intent.putExtra("addBook",addBook)
//            intent.putExtra("id",id)
//            intent.putExtra("talkWith",talkWith)
//            intent.putExtra("addBook",addBook)
            context.startActivity(intent)

        }
    }
}